<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>recordfunction_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_testing_recordfunction_test.js.html">recordfunction_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2010 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.testing.recordFunctionTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.testing.recordFunctionTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.functions&#39;);
<a name="line19"></a>goog.require(&#39;goog.testing.PropertyReplacer&#39;);
<a name="line20"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line21"></a>goog.require(&#39;goog.testing.recordConstructor&#39;);
<a name="line22"></a>goog.require(&#39;goog.testing.recordFunction&#39;);
<a name="line23"></a>
<a name="line24"></a>var stubs = new goog.testing.PropertyReplacer();
<a name="line25"></a>
<a name="line26"></a>function tearDown() {
<a name="line27"></a>  stubs.reset();
<a name="line28"></a>}
<a name="line29"></a>
<a name="line30"></a>function testNoCalls() {
<a name="line31"></a>  var f = goog.testing.recordFunction(goog.functions.identity);
<a name="line32"></a>  assertEquals(&#39;call count&#39;, 0, f.getCallCount());
<a name="line33"></a>  assertNull(&#39;last call&#39;, f.getLastCall());
<a name="line34"></a>  assertArrayEquals(&#39;all calls&#39;, [], f.getCalls());
<a name="line35"></a>}
<a name="line36"></a>
<a name="line37"></a>function testWithoutArguments() {
<a name="line38"></a>  var f = goog.testing.recordFunction();
<a name="line39"></a>  assertUndefined(&#39;f(1)&#39;, f(1));
<a name="line40"></a>  assertEquals(&#39;call count&#39;, 1, f.getCallCount());
<a name="line41"></a>  var lastCall = f.getLastCall();
<a name="line42"></a>  assertEquals(&#39;original function&#39;, goog.nullFunction, lastCall.getFunction());
<a name="line43"></a>  assertEquals(&#39;this context&#39;, window, lastCall.getThis());
<a name="line44"></a>  assertArrayEquals(&#39;arguments&#39;, [1], lastCall.getArguments());
<a name="line45"></a>  assertEquals(&#39;arguments[0]&#39;, 1, lastCall.getArgument(0));
<a name="line46"></a>  assertUndefined(&#39;arguments[1]&#39;, lastCall.getArgument(1));
<a name="line47"></a>  assertUndefined(&#39;return value&#39;, lastCall.getReturnValue());
<a name="line48"></a>}
<a name="line49"></a>
<a name="line50"></a>function testWithIdentityFunction() {
<a name="line51"></a>  var f = goog.testing.recordFunction(goog.functions.identity);
<a name="line52"></a>  var dummyThis = {};
<a name="line53"></a>  assertEquals(&#39;f(1)&#39;, 1, f(1));
<a name="line54"></a>  assertEquals(&#39;f.call(dummyThis, 2)&#39;, 2, f.call(dummyThis, 2));
<a name="line55"></a>
<a name="line56"></a>  var calls = f.getCalls();
<a name="line57"></a>  var firstCall = calls[0];
<a name="line58"></a>  var lastCall = f.getLastCall();
<a name="line59"></a>  assertEquals(&#39;call count&#39;, 2, f.getCallCount());
<a name="line60"></a>  assertEquals(&#39;last call&#39;, calls[1], lastCall);
<a name="line61"></a>  assertEquals(&#39;original function&#39;, goog.functions.identity,
<a name="line62"></a>      lastCall.getFunction());
<a name="line63"></a>  assertEquals(&#39;this context of first call&#39;, window, firstCall.getThis());
<a name="line64"></a>  assertEquals(&#39;this context of last call&#39;, dummyThis, lastCall.getThis());
<a name="line65"></a>  assertArrayEquals(&#39;arguments of the first call&#39;, [1],
<a name="line66"></a>      firstCall.getArguments());
<a name="line67"></a>  assertArrayEquals(&#39;arguments of the last call&#39;, [2], lastCall.getArguments());
<a name="line68"></a>  assertEquals(&#39;return value of the first call&#39;, 1, firstCall.getReturnValue());
<a name="line69"></a>  assertEquals(&#39;return value of the last call&#39;, 2, lastCall.getReturnValue());
<a name="line70"></a>  assertNull(&#39;error thrown by the first call&#39;, firstCall.getError());
<a name="line71"></a>  assertNull(&#39;error thrown by the last call&#39;, lastCall.getError());
<a name="line72"></a>}
<a name="line73"></a>
<a name="line74"></a>function testWithErrorFunction() {
<a name="line75"></a>  var f = goog.testing.recordFunction(goog.functions.error(&#39;error&#39;));
<a name="line76"></a>
<a name="line77"></a>  var error = assertThrows(&#39;f(1) should throw an error&#39;, function() {
<a name="line78"></a>    f(1);
<a name="line79"></a>  });
<a name="line80"></a>  assertEquals(&#39;error message&#39;, &#39;error&#39;, error.message);
<a name="line81"></a>  assertEquals(&#39;call count&#39;, 1, f.getCallCount());
<a name="line82"></a>  var lastCall = f.getLastCall();
<a name="line83"></a>  assertEquals(&#39;this context&#39;, window, lastCall.getThis());
<a name="line84"></a>  assertArrayEquals(&#39;arguments&#39;, [1], lastCall.getArguments());
<a name="line85"></a>  assertUndefined(&#39;return value&#39;, lastCall.getReturnValue());
<a name="line86"></a>  assertEquals(&#39;recorded error message&#39;, &#39;error&#39;, lastCall.getError().message);
<a name="line87"></a>}
<a name="line88"></a>
<a name="line89"></a>function testWithClass() {
<a name="line90"></a>  var ns = {};
<a name="line91"></a>  /** @constructor */
<a name="line92"></a>  ns.TestClass = function(num) {
<a name="line93"></a>    this.setX(ns.TestClass.identity(1) + num);
<a name="line94"></a>  };
<a name="line95"></a>  ns.TestClass.prototype.setX = function(x) {
<a name="line96"></a>    this.x = x;
<a name="line97"></a>  };
<a name="line98"></a>  ns.TestClass.identity = function(x) {
<a name="line99"></a>    return x;
<a name="line100"></a>  };
<a name="line101"></a>  var originalNsTestClass = ns.TestClass;
<a name="line102"></a>
<a name="line103"></a>  stubs.set(ns, &#39;TestClass&#39;, goog.testing.recordConstructor(ns.TestClass));
<a name="line104"></a>  stubs.set(ns.TestClass.prototype, &#39;setX&#39;,
<a name="line105"></a>      goog.testing.recordFunction(ns.TestClass.prototype.setX));
<a name="line106"></a>  stubs.set(ns.TestClass, &#39;identity&#39;,
<a name="line107"></a>      goog.testing.recordFunction(ns.TestClass.identity));
<a name="line108"></a>
<a name="line109"></a>  var obj = new ns.TestClass(2);
<a name="line110"></a>  assertEquals(&#39;constructor is called once&#39;, 1, ns.TestClass.getCallCount());
<a name="line111"></a>  var lastConstructorCall = ns.TestClass.getLastCall();
<a name="line112"></a>  assertArrayEquals(&#39;... with argument 2&#39;, [2],
<a name="line113"></a>      lastConstructorCall.getArguments());
<a name="line114"></a>  assertEquals(&#39;the created object&#39;, obj, lastConstructorCall.getThis());
<a name="line115"></a>  assertEquals(&#39;type of the created object&#39;, originalNsTestClass,
<a name="line116"></a>      obj.constructor);
<a name="line117"></a>
<a name="line118"></a>  assertEquals(&#39;setX is called once&#39;, 1, obj.setX.getCallCount());
<a name="line119"></a>  assertArrayEquals(&#39;... with argument 3&#39;, [3],
<a name="line120"></a>      obj.setX.getLastCall().getArguments());
<a name="line121"></a>  assertEquals(&#39;The x field is properly set&#39;, 3, obj.x);
<a name="line122"></a>
<a name="line123"></a>  assertEquals(&#39;identity is called once&#39;, 1,
<a name="line124"></a>      ns.TestClass.identity.getCallCount());
<a name="line125"></a>  assertArrayEquals(&#39;... with argument 1&#39;, [1],
<a name="line126"></a>      ns.TestClass.identity.getLastCall().getArguments());
<a name="line127"></a>}
<a name="line128"></a>
<a name="line129"></a>function testPopLastCall() {
<a name="line130"></a>  var f = goog.testing.recordFunction();
<a name="line131"></a>  f(0);
<a name="line132"></a>  f(1);
<a name="line133"></a>
<a name="line134"></a>  var firstCall = f.getCalls()[0];
<a name="line135"></a>  var lastCall = f.getCalls()[1];
<a name="line136"></a>  assertEquals(&#39;return value of popLastCall&#39;, lastCall, f.popLastCall());
<a name="line137"></a>  assertArrayEquals(&#39;1 call remains&#39;, [firstCall], f.getCalls());
<a name="line138"></a>  assertEquals(&#39;next return value of popLastCall&#39;, firstCall, f.popLastCall());
<a name="line139"></a>  assertArrayEquals(&#39;0 calls remain&#39;, [], f.getCalls());
<a name="line140"></a>  assertNull(&#39;no more calls to pop&#39;, f.popLastCall());
<a name="line141"></a>}
<a name="line142"></a>
<a name="line143"></a>function testReset() {
<a name="line144"></a>  var f = goog.testing.recordFunction();
<a name="line145"></a>  f(0);
<a name="line146"></a>  f(1);
<a name="line147"></a>
<a name="line148"></a>  assertEquals(&#39;Should be two calls.&#39;, 2, f.getCallCount());
<a name="line149"></a>
<a name="line150"></a>  f.reset();
<a name="line151"></a>
<a name="line152"></a>  assertEquals(&#39;Call count should reset.&#39;, 0, f.getCallCount());
<a name="line153"></a>}
<a name="line154"></a>
<a name="line155"></a>function testAssertCallCount() {
<a name="line156"></a>  var f = goog.testing.recordFunction(goog.functions.identity);
<a name="line157"></a>
<a name="line158"></a>  f.assertCallCount(0);
<a name="line159"></a>
<a name="line160"></a>  f(&#39;Poodles&#39;);
<a name="line161"></a>  f.assertCallCount(1);
<a name="line162"></a>
<a name="line163"></a>  f(&#39;Hopscotch&#39;);
<a name="line164"></a>  f.assertCallCount(2);
<a name="line165"></a>
<a name="line166"></a>  f.reset();
<a name="line167"></a>  f.assertCallCount(0);
<a name="line168"></a>
<a name="line169"></a>  f(&#39;Bedazzler&#39;);
<a name="line170"></a>  f.assertCallCount(1);
<a name="line171"></a>
<a name="line172"></a>  assertThrows(function() {
<a name="line173"></a>    f.assertCallCount(11);
<a name="line174"></a>  });
<a name="line175"></a>}
</pre>


</body>
</html>
